package com.test.learnaidl.binderimp;

import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.test.learnaidl.IOnNewPersonArrivedListener;
import com.test.learnaidl.PersonMannageInterface;
import com.test.learnaidl.bean.PersonBean;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class PersonMannageImpl extends PersonMannageInterface.Stub {
    private static String TAG = "服务端 AIDLBinderPoolService PersonMannageImp > ";
    private List<PersonBean> mPersonBeanList = new ArrayList<>();
    /*
       AIDL-接口注册/解注册
       https://www.yuque.com/jiajun-faizt/haag2g/nb3aur
   */
    private RemoteCallbackList<IOnNewPersonArrivedListener> mListener = new RemoteCallbackList<>();

    @Override
    public boolean add(PersonBean personBean) throws RemoteException {
        if (null == personBean) {
            Log.i("==", TAG + "add personBean = null");
            return false;
        }

        if (mPersonBeanList.contains(personBean)) {
            Log.i("==", TAG + "add 失败  mPersonBeanList.contains  = true ,  personBean = " + personBean.toString());
            return false;
        }

        mPersonBeanList.add(personBean);

        //调用接口通知客户端
        if (mListener != null) {
            int size = mListener.beginBroadcast();
            for (int i = 0; i < size; i++) {
                IOnNewPersonArrivedListener broadcastItem = mListener.getBroadcastItem(i);
                broadcastItem.onNewsPersonBeanArrived(personBean);
            }

            mListener.finishBroadcast();
        }

        Log.i("==", TAG + "add personBean = " + personBean.toString());

        return true;
    }

    @Override
    public boolean removeById(String id) throws RemoteException {
        if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
            Log.i("==", TAG + "remove   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true   id = " + id);
            return false;
        }

        ListIterator<PersonBean> personBeanListIterator = mPersonBeanList.listIterator();
        while (personBeanListIterator.hasNext()) {
            PersonBean personBean = personBeanListIterator.next();
            if (TextUtils.equals(personBean.getId(), id)) {

                boolean removeResult = mPersonBeanList.remove(personBean);

                if (removeResult) {
                    Log.i("==", TAG + "remove 成功 personBean = " + personBean.toString());

                } else {
                    Log.i("==", TAG + "remove 失败 id = " + id);
                }

                return removeResult;
            }
        }

        Log.i("==", TAG + "remove 失败 id = " + id);
        return false;
    }

    @Override
    public boolean remove(PersonBean personBean) throws RemoteException {
        if (null == personBean) {
            Log.i("==", TAG + "remove personBean = null");
            return false;
        }

        if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
            Log.i("==", TAG + "remove   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true   personBean = " + personBean.toString());
            return false;
        }

        boolean remove = mPersonBeanList.remove(personBean);

        if (remove) {
            Log.i("==", TAG + "remove 成功 personBean = " + personBean.toString());
        }


        return remove;
    }

    @Override
    public PersonBean getPersonBeanById(String id) throws RemoteException {
        if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
            Log.i("==", TAG + "getPersonBean   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true   id = " + id);
            return null;
        }
        for (PersonBean personBean : mPersonBeanList) {
            if (TextUtils.equals(personBean.getId(), id)) {
                Log.i("==", TAG + "getPersonBean 成功 personBean = " + personBean.toString());

                return personBean;
            }
        }

        Log.i("==", TAG + "getPersonBean 失败 id = " + id);
        return null;
    }

    @Override
    public List<PersonBean> getPersonBeans() throws RemoteException {
        if (mPersonBeanList == null || mPersonBeanList.isEmpty()) {
            Log.i("==", TAG + " 获取全部 getPersonBeans   mPersonBeanList == null 或者 mPersonBeanList.isEmpty() = true ");
        }
        return mPersonBeanList;
    }

    @Override
    public void registerListener(IOnNewPersonArrivedListener listener) throws RemoteException {
        if (listener == null) {
            Log.i("==", TAG + "registerListener  listener == null");
            return;
        }
        mListener.register(listener);
        Log.i("==", TAG + "registerListener ");
    }

    @Override
    public void unregisterListener(IOnNewPersonArrivedListener listener) throws RemoteException {
        if (listener == null) {
            Log.i("==", TAG + "unregisterListener  listener == null");
            return;
        }
        mListener.unregister(listener);
        Log.i("==", TAG + "unregisterListener ");
    }
}
